package com.oig.sys.log.service.impl;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysPageInfo;
import com.oig.common.module.SysRequest;
import com.oig.sys.log.dao.ITbSysLogDao;
import com.oig.sys.log.dto.SysLogDto;
import com.oig.sys.log.service.ITbSysLogService;
import com.oig.sys.log.entity.TbSysLog;

import com.oig.sys.log.util.LogQueryUtil;
import com.oig.sys.log.vo.SysLogVo;
import com.oig.sys.security.util.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author wuxl
 */
@Service
public class TbSysLogServiceImpl implements ITbSysLogService {

	private final ITbSysLogDao tbSysLogDao;
	private final ElasticsearchRestTemplate elasticsearchTemplate;

	@Autowired
	public TbSysLogServiceImpl(ITbSysLogDao tbSysLogDao, ElasticsearchRestTemplate elasticsearchTemplate) {
		this.tbSysLogDao = tbSysLogDao;
		this.elasticsearchTemplate = elasticsearchTemplate;
	}

	@Transactional
	@Override
	public void addSysLog(SysLogDto sysLogDto) {
		// 保存访问日子
		TbSysLog sysLog = new TbSysLog();
		BeanUtils.copyProperties(sysLogDto, sysLog);
		sysLog.setUserId(SecurityUtil.getUserForIfNullBuild().getId());
		sysLog.setUserName(SecurityUtil.getUserForIfNullBuild().getUsername());
		sysLog.setTenantId(SecurityUtil.getUserForIfNullBuild().getTenantId());
		sysLog.setTenantName(SecurityUtil.getUserForIfNullBuild().getTenantName());
		this.tbSysLogDao.save(sysLog) ;
	}

	@Override
	public SysPageInfo<SysLogVo> getSysLogInfo(SysRequest<SysLogDto> request) {
		// 1、生成查询ES日志的实体
		String optStr = "optTime";
		NativeSearchQueryBuilder builder = LogQueryUtil.buildQueryByDto(request, Lists.newArrayList(optStr));
		// 2、添加默认的页面页码和排序   0 代表第一页  按操作时间 倒序排
		NativeSearchQuery query = builder.withPageable(PageRequest.of(request.getPageNo() - 1, request.getPageSize()))
				.withSort(Sort.by(new Order(Sort.Direction.DESC, optStr)))
				.build();
		// 3、查询ES
		SearchHits<TbSysLog> search = elasticsearchTemplate.search(query, TbSysLog.class);
		// 4、结果转换Vo
		List<SysLogVo> result = search.getSearchHits().stream().map(item -> {
			TbSysLog content = item.getContent();
			SysLogVo sysLogVo = new SysLogVo();
			BeanUtils.copyProperties(content, sysLogVo);
			return sysLogVo;
		}).collect(Collectors.toList());
		SysPageInfo<SysLogVo> pageInfo = new SysPageInfo<>(request.getPageNo(), request.getPageSize(),
				search.getTotalHits());
		pageInfo.setList(result);
		return pageInfo;
	}

	@Override
	public SysLogVo getSysLogInfoById(String id) {
		Optional<TbSysLog> sysLogOpt = tbSysLogDao.findById(id);
		if (!sysLogOpt.isPresent()) {
			throw new BadRequestException("日志信息未找到");
		}
		SysLogVo sysLogVo = new SysLogVo();
		BeanUtils.copyProperties(sysLogOpt.get(), sysLogVo);
		return sysLogVo;
	}

}
